package com.mobileares.midp.widgets.client.textbox2.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import com.mobileares.midp.widgets.client.tree.CTree;
import com.mobileares.midp.widgets.client.tree.CTreeItem;
import com.mobileares.midp.widgets.client.utils.CodeModel;
import com.mobileares.midp.widgets.client.utils.ICodeProvider;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2012-1-6
 * Time: 9:11:50
 * To change this template use File | Settings | File Templates.
 */
public class TreeListBox extends Composite {
    private TextBox box = new TextBox() {
        @Override
        public void onBrowserEvent(Event event) {
            if (event.getTypeInt() == Event.ONBLUR) {
                if (pop.isShowing()) return;
            }
            super.onBrowserEvent(event);
        }
    };
    private PopupPanel pop = new PopupPanel(true);
    private CTree tree;

    private Object value = null;
    private ICodeProvider codeProvider = new ICodeProvider<CodeModel>() {
        public Object getUserObject(CodeModel ob) {
            return ob.getCode();
        }

        public Widget getWidget(CodeModel ob) {
            return new Label(ob.getText());
        }

        public String getText(CodeModel ob) {
            return ob.getText();
        }

        public String getCode(CodeModel ob) {
            return ob.getCode();
        }
    };

    public TreeListBox() {
        box.setStyleName("textbox-list");
        box.setReadOnly(true);
        tree = new CTree();
        pop.setStyleName("textbox-pop-div");
        DOM.setStyleAttribute(pop.getElement(), "zIndex", "1001");
        pop.add(tree);
        initWidget(box);
        init();
    }


    private void init() {
        box.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                pop.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop() + 26);
                pop.show();
                DeferredCommand.addCommand(new Command() {
                    public void execute() {
//                        listWidget.setFocus(true);
//                        listWidget.setSelected(value, false);
                        //todo:
                        tree.closedAll();
                    }
                });
            }
        });
        tree.addSelectionHandler(new SelectionHandler<CTreeItem>() {
            public void onSelection(SelectionEvent<CTreeItem> cTreeItemSelectionEvent) {
                Object ob = cTreeItemSelectionEvent.getSelectedItem().getUserObject();
                CTreeItem item = getItem(tree.getIterator(), codeProvider.getCode(ob));
                if (item.getChildren() != null && item.getChildren().size() > 0)
                    return;
                value = codeProvider.getUserObject(ob);
                pop.hide();
                setValue(value);
            }
        });
    }

    public Object getValue() {
        return value;
    }

    public void clear() {
        tree.clear();
    }


    public void setValue(Object value) {
        this.value = value;
        CTreeItem item = getItem(tree.getIterator(), value);
        if (item == null) box.setValue(value == null ? "" : (String) value, true);
        else
            box.setValue(codeProvider.getText(item.getUserObject()), true);
        box.setFocus(true);

    }

    public CTreeItem getItem(Iterator it, Object value) {
        for (; it.hasNext();) {
            CTreeItem item = (CTreeItem) it.next();
            if (codeProvider.getCode(item.getUserObject()).equals(value)) {
                return item;
            } else {
                if (item.getChildren() != null) {
                    CTreeItem child = getItem(item.getChildren().iterator(), value);
                    if (child != null) return child;
                }
            }
        }
        return null;
    }

    public CTreeItem addItem(Object ob) {
        CTreeItem item = new CTreeItem(codeProvider.getText(ob));
        item.setUserObject(ob);
        tree.addTreeItem(item);
        return item;
    }

    public CTreeItem addItem(CTreeItem parent, Object ob) {
        CTreeItem item = new CTreeItem(codeProvider.getText(ob));
        item.setUserObject(ob);
        parent.addTreeItem(item);
        return item;
    }

    public Widget getWidget() {
        return box;
    }

    public void setCodeProvider(ICodeProvider codeProvider) {
        this.codeProvider = codeProvider;
    }
}
